package jsyf.oa.service.report.intentCustomer;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.intentCustomer.ReportCustomerDao;
import jsyf.oa.entity.report.intentCustomer.ReportCustomerEntity;
import jsyf.oa.facade.report.intentCustomer.ReportCustomerFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/5.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class ReportCustomerService implements ReportCustomerFacade {

    @Autowired
    private ReportCustomerDao reportCustomerDao;
    @Autowired
    private ReportCommonMethodsAndConstant common;

    /*获取个人*/
    @Override
    public JSONObject getForPersonalIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = "";
        if (jsonParams.containsKey("key")) {
            key = jsonParams.getString("key");
        }
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        List<JSONObject> records = reportCustomerDao.getForPersonalIntentCustomerList(userId, key, requestPage, pageSize);
        int recordCount = reportCustomerDao.getCountPersonalIntentCustomerList(userId, key);
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    /*部门*/
    @Override
    public List<JSONObject> getForDepartIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return reportCustomerDao.getForDepartIntentCustomerList(userId);
    }

    /*公司*/
    @Override
    public List<JSONObject> getForCompanyIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return reportCustomerDao.getForCompanyIntentCustomerList(userId);
    }

    /*集团*/
    @Override
    public List<JSONObject> getForGroupIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = common.getAllCompanysIdByUserId(userId);
        return reportCustomerDao.getForGroupIntentCustomerList(companyId);
    }

    /*获取所有*/
    @Override
    public JSONObject getAllReportCustomer(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        /*获取身份标志*/
        JSONObject roleFlag = reportCustomerDao.getManageFlagAndFrontFlagAndAreaTypeByUserId(userId);
            /*获取集团人员可以看到的公司*/
        String companyId = common.getAllCompanysIdByUserId(userId);
           /*获取所有意向客户*/
        List<JSONObject> records = reportCustomerDao.getAllIntentCustomerList(userId, key, companyId, requestPage, pageSize, roleFlag);
          /*获得查询数目*/
        int recordCount = reportCustomerDao.getCountByCompanyIdAndUserIdAndKey(companyId, userId, key, roleFlag);

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    /*集团页面点击公司*/
    @Override
    public List<JSONObject> getForDepartByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        return reportCustomerDao.getForDepartByCompanyId(companyId);
    }

    /*获取APP大区意向客户*/
    @Override
    public List<JSONObject> getForAppBigRegionIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return reportCustomerDao.getForAppBigRegionIntentCustomerList(userId);

    }

    @Override
    public JSONObject getForAppIntentCustomerListsByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        /*查询数目*/
        int recordCount = reportCustomerDao.getCountForAppIntentCustomerListsByUserId(userId);
        List<JSONObject> customerList = reportCustomerDao.getForAppIntentCustomerListsByUserId(userId);
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", customerList);
        return result;
    }

    @Override
    public JSONObject saveIntentCustomerInfo(String jsonData) {
        ReportCustomerEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), ReportCustomerEntity.class);
        Integer rows = 0;
        JSONObject obj = new JSONObject();
        if (entity.getIntentionModel() != null) {
            entity.setIntentionModel(entity.getIntentionModel().toUpperCase());
        }
        if (entity.getExistingDevice() != null) {
            entity.setExistingDevice(entity.getExistingDevice().toUpperCase());
        }
        if (org.apache.commons.lang3.StringUtils.isBlank(entity.getId()) || "0".equals(entity.getId())) {
            entity.setIntentionTime(reportCustomerDao.getCurrentTime());
            String tel = entity.getTel().replace(" ", "");
            if (org.apache.commons.lang3.StringUtils.isBlank(reportCustomerDao.getForTel(tel))) {
                entity.setTel(tel);
                rows = reportCustomerDao.add(entity);
                if (rows > 0) {
                    obj.put("records", "保存成功");
                } else {
                    obj.put("records", "保存失败");
                }

            } else {
                obj.put("records", "电话号码已存在");
            }

        } else {
                rows = reportCustomerDao.update(entity);
                if (rows > 0) {
                    obj.put("records", "保存成功");
                } else {
                    obj.put("records", "保存失败");
                }
        }
        return obj;
    }

    @Override
    public JSONObject getForUserInfo(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        List<JSONObject> userList = reportCustomerDao.getForUserInfo(userId);
        JSONObject result = new JSONObject();
        result.put("records", userList);
        return result;
    }

    @Override
    public JSONObject getForProvinceById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        List<JSONObject> provinceList = reportCustomerDao.getForProvinceById(id);
        JSONObject result = new JSONObject();
        result.put("records", provinceList);
        return result;
    }

}